<?php
/**
 * 自定义文章类型：幻灯片
 * @package WordPress
 * @subpackage wheatv
 */

/*Create Slider Post Type*/
function create_slider() {
  $labels = array(
    'name' => _x('幻灯片', 'post type general name'),
    'singular_name' => _x('Slider', 'post type singular name'),
    'add_new' => _x('添加幻灯片', 'slide'),
    'add_new_item' => __('添加新幻灯片'),
    'edit_item' => __('编辑幻灯片'),
    'new_item' => __('添加新幻灯片'),
    'view_item' => __('浏览幻灯片'),
    'search_items' => __('搜索幻灯片'),
    'not_found' =>  __('还没有添加幻灯片'),
    'not_found_in_trash' => __('垃圾箱中没有幻灯片'),
    'parent_item_colon' => ''
  );

  $supports = array('title','thumbnail');
  register_post_type( 'slider',
    array(
      'labels' => $labels,
      'public' => true,
      'supports' => $supports,
	  'menu_position' => 5,
	  'show_in_nav_menus'=>false,
	  'has_archive' => true
    )
  );
}

add_action( 'init', 'create_slider' );


/*******************************
   自定义字段面板
********************************/

$new_meta_boxes =
array(		
	"sliderlink" => array(
        "name" => "sliderlink",
	    "types" => "text",
        "title" => "图片链接指向:"),
);

function wheatv_new_meta_boxes() {
    global $post, $new_meta_boxes;
    foreach($new_meta_boxes as $meta_box) {
        $meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
        if($meta_box_value == "")
            $meta_box_value = $meta_box['std'];
        echo'<input type="hidden" name="'.$meta_box['name'].'_noncename" id="'.$meta_box['name'].'_noncename" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
        echo'<h4>'.$meta_box['title'].'</h4>';
		switch($meta_box['types']){
		  case 'textarea':
            echo '<textarea cols="80" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />';
			break;

		  case 'text':
		    echo '<input type="text" name="'.$meta_box['name'].'_value" value="'.$meta_box_value.'" style="width:600px" /><br />';
			break;

		 case 'checkbox':
		    foreach ($meta_box['options'] as $option){
				 $checkyes = '';
				if(strpos($meta_box_value, $option) !== false) {
					$checkyes = 'checked="checked"';
				}

		       echo'<input type="checkbox" name="'.$meta_box['name'].'_value[]" value="'.$option.'" '.$checkyes.' />'.$option.'&nbsp;&nbsp;';
			}

			break;
		 case 'radio':
		    foreach ($meta_box['options'] as $option){
		  ?>
		       <input type="radio" name="<?php echo $meta_box['name']?>_value" value="<?php echo $option;?>" <?php if ($option==$meta_box_value) { echo ' checked="checked"'; } ?> /> <?php echo $option; ?> &nbsp;&nbsp;
		  <?php	
            }
			break;
		}
    }
}

function wheatv_create_meta_box() {
    global $theme_name;
    if ( function_exists('add_meta_box') ) {
        add_meta_box( 'new-meta-boxes', '幻灯片设置', 'wheatv_new_meta_boxes', 'slider', 'normal', 'high' );
    }
}
function wheatv_save_postdata( $post_id ) {
    global $post, $new_meta_boxes;
    foreach($new_meta_boxes as $meta_box) {
        if ( !wp_verify_nonce( $_POST[$meta_box['name'].'_noncename'], plugin_basename(__FILE__) ))  {
            return $post_id;
        }

        if ( 'page' == $_POST['post_type'] ) {
            if ( !current_user_can( 'edit_page', $post_id ))
               return $post_id;
        } 
        else {
            if ( !current_user_can( 'edit_post', $post_id ))
                return $post_id;
        }

        //$data = $_POST[$meta_box['name'].'_value'];
		if ($meta_box['types'] == 'checkbox' && is_array($_POST[$meta_box['name'].'_value'])) {
			$data = implode(",",$_POST[$meta_box['name'].'_value']);
		}else {
			$data = $_POST[$meta_box['name'].'_value'];
		}

        if(get_post_meta($post_id, $meta_box['name'].'_value') == "")
            add_post_meta($post_id, $meta_box['name'].'_value', $data, true);
        elseif($data != get_post_meta($post_id, $meta_box['name'].'_value', true))
            update_post_meta($post_id, $meta_box['name'].'_value', $data);
        elseif($data == "")
            delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
    }

}

add_action('admin_menu', 'wheatv_create_meta_box');
add_action('save_post', 'wheatv_save_postdata');
?>